<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="登录名" prop="loginname">
        <el-input
          v-model.trim="form.loginname"
          maxlength="20"
          show-word-limit
        />
      </el-form-item>
      <el-form-item label="密码" prop="loginpwd">
        <el-input v-model.trim="form.loginpwd" type="password" />
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input v-model.trim="form.name" maxlength="10" show-word-limit />
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-radio v-model="form.sex" label="1">男</el-radio>
        <el-radio v-model="form.sex" label="2">女</el-radio>
      </el-form-item>
      <el-form-item label="角色" prop="isAdmin">
        <el-radio v-model="form.isAdmin" label="true">管理员</el-radio>
        <el-radio v-model="form.isAdmin" label="false">普通用户</el-radio>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio v-model="form.status" label="1">正常</el-radio>
        <el-radio v-model="form.status" label="2">禁用</el-radio>
      </el-form-item>
      <el-form-item label="最后登录IP">
        <el-input v-model.trim="form.lastLoginIp" />
      </el-form-item>
      <el-form-item label="最后登录时间">
        <el-input v-model.trim="form.lastLoginTime" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import { doEdit } from '@/api/userManagement'

  export default {
    name: 'UserManagementEdit',
    data() {
      return {
        form: {},
        rules: {
          loginname: [
            { required: true, trigger: 'blur', message: '请输入登录名' },
            {
              min: 3,
              max: 20,
              message: '登录名长度在 3 到 20 个字符',
              trigger: 'blur',
            },
          ],
          loginpwd: [
            { required: true, trigger: 'blur', message: '请输入密码' },
            {
              min: 1,
              max: 20,
              message: '密码长度在 1 到 20 个字符',
              trigger: 'blur',
            },
          ],
          name: [
            { required: true, trigger: 'blur', message: '请输入姓名' },
            {
              min: 1,
              max: 20,
              message: '姓名长度在 1 到 10 个字符',
              trigger: 'blur',
            },
          ],
          isAdmin: [{ required: true, trigger: 'blur', message: '请选择角色' }],
          status: [{ required: true, trigger: 'blur', message: '请选择状态' }],
          sex: [{ required: true, trigger: 'blur', message: '请选择性别' }],
        },
        title: '',
        dialogFormVisible: false,
      }
    },
    methods: {
      showEdit(row) {
        if (!row) {
          this.title = '添加'
        } else {
          this.title = '编辑'
          this.form = Object.assign({}, row)
          this.form.status = this.form.status + ''
          this.form.isAdmin = this.form.isAdmin + ''
          this.form.sex = this.form.sex + ''
        }
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            const { msg } = await doEdit(this.form)
            this.$baseMessage(msg, 'success', false, 'vab-hey-message-success')
            this.$emit('fetch-data')
            this.close()
          } else {
            return false
          }
        })
      },
    },
  }
</script>
